package com.stu.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.stu.dao.CustomerDao;
import com.stu.domain.Customer;
import com.stu.exception.DaoException;
import com.stu.exception.IdNullException;
import com.stu.utils.JDBCUtil;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	public void addCustomer(Customer c) {
		// TODO Auto-generated method stub
		if (c==null) {
			throw new IllegalArgumentException();
		}
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("insert into customer (id,name,gender,birthday,cellphone,email,hobby,type,description) values(?,?,?,?,?,?,?,?,?)");
			ps.setString(1, c.getId());
			ps.setString(2, c.getName());
			ps.setString(3, c.getGender());
			ps.setDate(4, new Date(c.getBirthday().getTime()));
			ps.setString(5, c.getCellphone());
			ps.setString(6, c.getEmail());
			ps.setString(7, c.getHobby());
			ps.setString(8, c.getType());
			ps.setString(9, c.getDescription());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
	}

	@Override
	public void delCustomerById(String id) {
		// TODO Auto-generated method stub
		if (id==null) {
			throw new IllegalArgumentException();
		}
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("delete from  customer  where id=?");
			ps.setString(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
	}

	@Override
	public void updateCustomer(Customer c) throws IdNullException {
		// TODO Auto-generated method stub
		if (c==null) {
			throw new IllegalArgumentException();
		}
		if (c.getId()==null) {
			throw new IdNullException();
		}
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("update customer set name=?,gender=?,birthday=?,cellphone=?,email=?,hobby=?,type=?,description=? where id=?");
		
			ps.setString(1, c.getName());
			ps.setString(2, c.getGender());
			ps.setDate(3, new Date(c.getBirthday().getTime()));
			ps.setString(4, c.getCellphone());
			ps.setString(5, c.getEmail());
			ps.setString(6, c.getHobby());
			ps.setString(7, c.getType());
			ps.setString(8, c.getDescription());
			ps.setString(9, c.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
	}

	@Override
	public List<Customer> findAll() {
		// TODO Auto-generated method stub
		
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Customer> list=new ArrayList<Customer>();
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from customer ");
			rs=ps.executeQuery();
			while (rs.next()) {
				Customer c=new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setGender(rs.getString("gender"));
				c.setBirthday(rs.getDate("birthday"));
				c.setCellphone(rs.getString("cellphone"));
				c.setEmail(rs.getString("email"));
				c.setHobby(rs.getString("hobby"));
				c.setType(rs.getString("type"));
				c.setDescription(rs.getString("description"));
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
		return list;
	}

	@Override
	public Customer findCustomerById(String id) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Customer c=null;
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from customer where id=? ");
			ps.setString(1, id);
			rs=ps.executeQuery();
			if (rs.next()) {
				 c=new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setGender(rs.getString("gender"));
				c.setBirthday(rs.getDate("birthday"));
				c.setCellphone(rs.getString("cellphone"));
				c.setEmail(rs.getString("email"));
				c.setHobby(rs.getString("hobby"));
				c.setType(rs.getString("type"));
				c.setDescription(rs.getString("description"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
		return c;
	}

	@Override
	public int getTotalRecords() {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Customer> list=new ArrayList<Customer>();
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("select count(*) from customer");
			rs=ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
		 
		return 0;
	}

	@Override
	public List<Customer> findPageRecords(int startIndex, int pagesize) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Customer> list=new ArrayList<Customer>();
		try {
			conn=JDBCUtil.getConnection();
			ps=conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from customer limit ?,?");
			ps.setInt(1, startIndex);
			ps.setInt(2, pagesize);
			rs=ps.executeQuery();
			while (rs.next()) {
				Customer c=new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setGender(rs.getString("gender"));
				c.setBirthday(rs.getDate("birthday"));
				c.setCellphone(rs.getString("cellphone"));
				c.setEmail(rs.getString("email"));
				c.setHobby(rs.getString("hobby"));
				c.setType(rs.getString("type"));
				c.setDescription(rs.getString("description"));
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DaoException(e);
		}finally{
			JDBCUtil.release(rs, ps, conn);
		}
		return list;
	}

}
